//
//  NSDate+AFF.m
//  AnyfishApp
//
//  Created by Bob Lee on 15/9/6.
//  Copyright (c) 2015年 Anyfish. All rights reserved.
//

#import "NSDate+AFF.h"
#import "regex.h"
#import "NSDate+Formatter.h"

@implementation NSDate (AFF)

+ (long)timeMicro {
    return [[NSDate date] timeIntervalSince1970]*1000000;
}

+ (long)timeSecond {
    return [[NSDate date] timeIntervalSince1970];
}

+ (NSDate *)string2Date:(NSString *) str {
    NSDateFormatter *dateFormatter = [NSDate formatter];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSDate *dateTime = [dateFormatter dateFromString:str];
    return dateTime;
}

+ (NSString *)currentTimeString:(TimeFormatType) type {
    NSDateFormatter *dateFormatter = [NSDate formatter];
    if(type == ETimeFormatTimeDate){
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    else if(type == ETimeFormatTimeCommon){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    else if(type == ETimeFormatTimeShort){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }else if(type == ETimeFormatTimeTime){
        [dateFormatter setDateFormat:@"HH:mm"];
    }else if(type == ETimeFormatTimeYear){
        [dateFormatter setDateFormat:@"yyyy"];
    }else if(type == ETimeFormatTimeMonth){
        [dateFormatter setDateFormat:@"MM"];
    }else if(type == ETimeFormatTimeDay){
        [dateFormatter setDateFormat:@"dd"];
    }else if(type == ETimeFormatTimeHour){
        [dateFormatter setDateFormat:@"HH"];
    }else if(type == ETimeFormatTimeMinute){
        [dateFormatter setDateFormat:@"mm"];
    }else if(type == ETimeFormatTimeSecond){
        [dateFormatter setDateFormat:@"ss"];
    }else if(type == ETimeFormatTimeLong){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    }else if(type == ETimeFormatTimeMask){
        [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    }else if(type == ETimeFormatTimeShortDate){
        [dateFormatter setDateFormat:@"MM-dd"];
    }else if(type == ETimeFormatTimeShortDateTime){
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    }else if(type == ETimeFormatTimeShortDateTime_CN){
        [dateFormatter setDateFormat:@"M月d日 HH:mm"];
    }else if(type == ETimeFormatTimeShortDate_CN){
        [dateFormatter setDateFormat:@"M月d日"];
    }else if(type == ETimeFormatTimeDateDotSpan){
        [dateFormatter setDateFormat:@"yyyyMMdd"];
    }else if(type == ETimeFormatTimeYearMonth){
        [dateFormatter setDateFormat:@"yyyy年MM月"];
    }
    
    NSString *curTime = [dateFormatter stringFromDate:[NSDate date]];
    
    return curTime;
}

+ (NSString *)date2StringWithInterval:(NSInteger)interval formatType:(TimeFormatType)type {
    return [self date2String:[NSDate dateWithTimeIntervalSince1970:interval] formatType:type];
}

+ (NSString *)date2String:(NSDate *)date formatType:(TimeFormatType)type {
    if(date==nil)
        return nil;
    
    NSDateFormatter *dateFormatter = [NSDate formatter];
    
    if(type == ETimeFormatTimeDate){
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    else if(type == ETimeFormatTimeDateEx)
    {
        [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    }
    else if(type == ETimeFormatTimeCommon){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    else if(type == ETimeFormatTimeShort){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }else if(type == ETimeFormatTimeTime){
        [dateFormatter setDateFormat:@"HH:mm"];
    }else if(type == ETimeFormatTimeYear){
        [dateFormatter setDateFormat:@"yyyy"];
    }else if(type == ETimeFormatTimeMonth){
        [dateFormatter setDateFormat:@"MM"];
    }else if(type == ETimeFormatTimeDay){
        [dateFormatter setDateFormat:@"dd"];
    }else if(type == ETimeFormatTimeHour){
        [dateFormatter setDateFormat:@"HH"];
    }else if(type == ETimeFormatTimeMinute){
        [dateFormatter setDateFormat:@"mm"];
    }else if(type == ETimeFormatTimeSecond){
        [dateFormatter setDateFormat:@"ss"];
    }else if(type == ETimeFormatTimeLong){
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    }else if(type == ETimeFormatTimeMask){
        [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    }else if(type == ETimeFormatTimeShortDate){
        [dateFormatter setDateFormat:@"MM-dd"];
    }else if(type == ETimeFormatTimeShortDateTime){
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    }else if(type == ETimeFormatTimeShortDateTime_CN){
        [dateFormatter setDateFormat:@"M月d日 HH:mm"];
    }else if(type == ETimeFormatTimeShortDate_CN){
        [dateFormatter setDateFormat:@"M月d日"];
    }else if(type == ETimeFormatTimeDateDotSpan){
        [dateFormatter setDateFormat:@"yyyyMMdd"];
    }else if(type == ETimeFormatTimeYearMonth){
        [dateFormatter setDateFormat:@"yyyy年MM月"];
    }
    
    NSString *curTime = [dateFormatter stringFromDate:date];
    
    return curTime;
}

+ (NSString*)date2String:(NSDate *)date {
    return [self date2String:date isMessage:NO];
}

+ (NSString *)date2String:(NSDate*)date isMessage:(BOOL)isMessage {
//    NSString *formatStr = nil;
//    
//    //1.获取当前时间所在的年月日，时分
//    NSString *dateStr = [self currentTimeString:ETimeFormatTimeShort];
//    NSArray *dateArr = [[dateStr componentsSeparatedByString:@" "][0] componentsSeparatedByString:@"-"];
//    NSArray *timeArr = [[dateStr componentsSeparatedByString:@" "][1] componentsSeparatedByString:@":"];
//    NSInteger yearNow = [[dateArr[0] substringFromIndex:2] integerValue],monthNow = [dateArr[1] integerValue],dayNow = [dateArr[2] integerValue];
//    //    NSInteger hourNow = [timeArr[0] integerValue],minuteNow = [timeArr[1] integerValue];
//    
//    
//    //2.获取目标时间所在年月日，时分
//    dateStr = [self date2String:date formatType:ETimeFormatTimeShort];
//    
//    if([NSString isNilOrEmpty:dateStr]){
//        return @"时间格式错误";
//    }
//    
//    dateArr = [[dateStr componentsSeparatedByString:@" "][0] componentsSeparatedByString:@"-"];
//    timeArr = [[dateStr componentsSeparatedByString:@" "][1] componentsSeparatedByString:@":"];
//    NSInteger yearTarget = [[dateArr[0] substringFromIndex:2] integerValue],monthTarget = [dateArr[1] integerValue],dayTarget = [dateArr[2] integerValue];
//    NSInteger hourTarget = [timeArr[0] integerValue],minuteTarget = [timeArr[1] integerValue];
//    
//    //3.判断是否本年
//    if(yearNow>yearTarget){
//        if((abs(12-monthTarget+monthNow)>3) || (yearNow-1)>yearTarget){
//            formatStr = [NSString stringWithFormat:@"%02d-%02d-%02d",yearTarget,monthTarget,dayTarget];
//        }
//    }else{
//        if(monthNow==monthTarget){
//            if(dayNow-2 == dayTarget){
//                // 前天
//                formatStr = [NSString stringWithFormat:@"%@ %02d:%02d",kAFLocalize(kStrBefYesterday),hourTarget,minuteTarget];
//            }else if(dayNow-1 == dayTarget){
//                // 昨天
//                formatStr = [NSString stringWithFormat:@"%@ %02d:%02d",kAFLocalize(kStrYesterday),hourTarget,minuteTarget];
//            }else if(dayNow == dayTarget){
//                // 当天
//               // formatStr = [NSString stringWithFormat:@"%02d:%02d",hourTarget,minuteTarget];
//            }
//        }
//        
//        
//    }
//    
//    if(formatStr==nil){
////        if(isMessage)
////            formatStr = [NSString stringWithFormat:@"%02d-%02d", monthTarget,dayTarget];
////        else
//            formatStr = [NSString stringWithFormat:@"%02d-%02d %02d:%02d", monthTarget,dayTarget,hourTarget,minuteTarget];
//    }
//    
//    return formatStr;
    return nil;
}

+ (NSString *)date2StringWithInterval:(NSInteger)interval isMessage:(BOOL)isMessage {
    return [self date2String:[NSDate dateWithTimeIntervalSince1970:interval] isMessage:isMessage];
}

+ (NSString*)date2StringWithInterval:(NSInteger)interval {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return [self date2String:date];
}

+ (WeekDayType)getWeekDay:(NSDate *)date {
    NSInteger week = [date weekday];

    WeekDayType weekDayType = EWeekDayTypeMonday;
    if(week==1)
    {
        weekDayType = EWeekDayTypeSunDay;
    }else if(week==2){
        weekDayType = EWeekDayTypeMonday;
    }else if(week==3){
        weekDayType = EWeekDayTypeTuesday;
    }else if(week==4){
        weekDayType = EWeekDayTypeWednesday;
    }else if(week==5){
        weekDayType = EWeekDayTypeThursday;
    }else if(week==6){
        weekDayType = EWeekDayTypeFriday;
    }else if(week==7){
        weekDayType = EWeekDayTypeSaturday;
    }
    return weekDayType;
}

+ (WeekDayType)getCurrentWeekDay:(NSString **) desc {
    //得到当前的日期--得到星期几
    NSDate *date = [NSDate date];
    
    WeekDayType weekType = [self getWeekDay:date];
    return weekType;
}

+ (NSMutableArray *)getDateArray:(NSUInteger)number {
    NSMutableArray* dateArray = [NSMutableArray arrayWithCapacity:0];
    NSDate* date = [NSDate date];
    for (NSInteger index = 0;index < number ;index++) {
        //转化时间（时间格式器）
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        //设置格式：yyyy表示年（两位yy和四位yyyy）MM表示月 dd标识天 hh表示小时 mm表示分钟 ss表示秒
        [formatter setDateFormat:@"yyyy年MM月"];
        NSString *string = [formatter stringFromDate:[date offsetMonths:-(int)index]];
        [dateArray addObject:string];
    }
    return dateArray;
}
+ (NSString *)getMonthString:(NSString *)string{
    NSString* year = [string substringWithRange:NSMakeRange(0, 4)];
    NSString* month = [string substringWithRange:NSMakeRange(5, 2)];
    return [year stringByAppendingString:month];
}
@end
